//
//  ARCTest.m
//  Charles
//
//  Created by Leo on 2017/5/24.
//  Copyright © 2017年 charles. All rights reserved.
//

#import "ARCTest.h"

@interface ARCTest()

@property (nonatomic,assign)void(^block)();
/*
 @property (nonatomic,weak)void(^block)();问题：为什么arc下
 按钮事件里面打印这个block 输出null 而不是崩溃
*/

@end


@implementation ARCTest

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpView];
    }
    return self;
}

- (void)setUpView {
    
    UIButton * testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    testButton.backgroundColor = [UIColor redColor];
    [testButton setTitle:@"ARC" forState:UIControlStateNormal];
    [self addSubview:testButton];

    [testButton makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(50);
        make.top.equalTo(100);
    }];
    [testButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)test {
    [self blockTest];
}

- (void)blockTest {
    int value = 10;
    void(^blockC)() = ^(){
        NSLog(@"只是一个block == %d",value);
    };
    NSLog(@"blockC is %@",blockC);
    _block = blockC;
    NSLog(@"block is %@",_block);
}

- (void)buttonEvent:(UIButton *)sender {
    // 属性用assign 声明block 这里会崩溃 因为blockTest里面声明的blcokC是一个临时变量，
    // 到button事件里面已经被释放了，此时_block变成了野指针
    //如果用weak 声明block 输出null 而不会崩溃，是因为weak 是弱引用
    NSLog(@"block is %@",_block);
}

@end
